package com.fht.service.food.impl;

import com.fht.constant.ManagermentSystemConstant;
import com.fht.entity.FoodSpecification;
import com.fht.entity.FoodTasteCategory;
import com.fht.enums.ManagermentSystemEnum;
import com.fht.exception.ManagermentSystemException;
import com.fht.mapper.food.FoodTasteCategoryMapper;
import com.fht.service.food.FoodRecordService;
import com.fht.service.food.FoodTasteCategoryService;
import com.fht.vo.LayUITableVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Slf4j
@Service
public class FoodTasteCategoryServiceImpl implements FoodTasteCategoryService {

    @Resource
    public FoodTasteCategoryMapper foodTasteCategoryMapper;

    @Autowired
    public FoodRecordService foodRecordService;

    @Override
    public LayUITableVO<List<FoodTasteCategory>> findPage(Map<String, Object> map) {


        PageHelper.startPage((int) map.get("page"),(int) map.get("limit"));

        List<FoodTasteCategory> list = foodTasteCategoryMapper.findPage(map);

        PageInfo<FoodTasteCategory> pageInfo = new PageInfo<>(list);
        List<FoodTasteCategory> foodTasteCategoryList = pageInfo.getList();
        for (FoodTasteCategory foodTasteCategory : foodTasteCategoryList) {
            Example example = new Example(FoodTasteCategory.class);
            Example.Criteria c = example.createCriteria();
            c.andEqualTo("recordId",foodTasteCategory.getRecordId());
            List<FoodTasteCategory> foodTasteCategories = foodTasteCategoryMapper.selectByExample(example);
            if (foodTasteCategories.size()>0) {
                String tasteNames = "";
                for (FoodTasteCategory tasteCategory : foodTasteCategories) {
                    tasteNames += tasteCategory.getTasteName() + "、";
                }
                tasteNames = tasteNames.substring(0, tasteNames.lastIndexOf("、"));
                foodTasteCategory.setTasteNames(tasteNames);
            }else {
                foodTasteCategory.setTasteNames("暂无数据");
            }
        }

        LayUITableVO vo = new LayUITableVO();
        vo.setCount(pageInfo.getTotal());
        vo.setData(foodTasteCategoryList);


        return vo;

    }

    @Override
    public List<FoodTasteCategory> selectCategoryName() {
        Example example = new Example(FoodTasteCategory.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("categoryId","0");
        List<FoodTasteCategory> foodTasteCategories = foodTasteCategoryMapper.selectByExample(example);
        return foodTasteCategories;
    }

    @Override
    public LayUITableVO<List<FoodTasteCategory>> selectByCategoryId(String categoryId,String recordId) {
        Example example = new Example(FoodTasteCategory.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("categoryId",categoryId);
        c.andEqualTo("recordId",recordId);
        List<FoodTasteCategory> foodTasteCategories = foodTasteCategoryMapper.selectByExample(example);

        for (FoodTasteCategory foodTasteCategory : foodTasteCategories) {
            Integer addPrice = foodTasteCategory.getAddPrice();
            if (addPrice!=0){
                foodTasteCategory.setAddPriceString(String.valueOf(addPrice/ ManagermentSystemConstant.PRICE_TRANSITION));
            }else {
                foodTasteCategory.setAddPriceString("0");
            }
        }


        LayUITableVO<List<FoodTasteCategory>> vo = new LayUITableVO<>();
        vo.setData(foodTasteCategories);
        return vo;
    }

    @Override
    @Transactional
    public void add(FoodTasteCategory foodTasteCategory) {

        Example example = new Example(FoodTasteCategory.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("recordId",foodTasteCategory.getRecordId());
        c.andEqualTo("tasteName",foodTasteCategory.getTasteName());
        List<FoodTasteCategory> foodTasteCategories = foodTasteCategoryMapper.selectByExample(example);
        if (foodTasteCategories.size()>0){

            throw new ManagermentSystemException(ManagermentSystemEnum.FOOD_CATEGORY_ADD_REPETITION);
        }


        String addPriceString = foodTasteCategory.getAddPriceString();
        foodTasteCategory.setAddPrice((int)( Double.valueOf(addPriceString)*ManagermentSystemConstant.PRICE_TRANSITION));

        foodTasteCategoryMapper.insertSelective(foodTasteCategory);
    }

    @Override
    @Transactional
    public void delelteById(String id) {
        foodTasteCategoryMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void update(FoodTasteCategory foodTasteCategory) {
        String addPriceString = foodTasteCategory.getAddPriceString();
        foodTasteCategory.setAddPrice((int)( Double.valueOf(addPriceString)*ManagermentSystemConstant.PRICE_TRANSITION));

        foodTasteCategoryMapper.updateByPrimaryKeySelective(foodTasteCategory);
    }
}
